Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice_things/utility/basename.sh

basename

Since 0.3.0 · Source

import "{ basename }" from nice_things/utility/basename.sh

Synopsis
basename <pathname> [<suffix>]

Configuration

Description
Print <pathname> with any leading directory components removed. If specified, also remove a trailing <suffix>.

This is a pure sh implementation of the basename utility specified in POSIX.

Being an implementation of a CLI utility, this function prints the result to stdout. If you want to assign the result to a variable, consider using the faster file_name function instead.

Options

Operands

  • <pathname>: A path name.
  • <suffix>: An optional suffix.

Stdin

Stdout
The resulting filename is printed to stdout, trailed by a line-feed character.

Stderr

Exit status
0: Successful completion.

Abort

Usage examples

# Print file name with extension removed
basename include/stdio.h .h # stdio